package com.gxa.saas.controller;

import com.gxa.saas.pojo.Warehouse;
import com.gxa.saas.service.WarehouseService;
import com.gxa.saas.utils.JsonResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: yx
 * @Date: 2022/6/7 14:43
 */
@RestController
@RequestMapping(value = "/warehouse")
@CrossOrigin
public class WarehouseController {

    @Autowired
    WarehouseService warehouseService;

    /**
     * 查询全部
     * @return
     */
    @ApiOperation("查询全部")
    @PostMapping("/selectAll")
    public Map selectAll(){
        List<Warehouse> warehouses = warehouseService.selectAll();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(warehouses);
        return jsonResult.getMap();
    }

    @ApiOperation(value = "分页查询仓库", produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "offset", value = "查询页数", paramType = "query")
    })
    @PostMapping("/selectByPage")
    public Map selectByPage(@RequestParam("offset") Integer offset) {
        List<Warehouse> warehouses = warehouseService.selectByPage(offset);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(warehouses);
        return jsonResult.getMap();
    }

    @ApiOperation(value = "添加仓库")
    @ApiImplicitParam(name = "warehouse", value = "要添加的仓库对象", paramType = "body", dataType = "Warehouse")
    @PostMapping("/insert")
    public Map insert(@RequestBody Warehouse warehouse) {
        warehouseService.insert(warehouse);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");
        return jsonResult.getMap();
    }

    @ApiOperation(value = "修改仓库信息", produces = "update")
    @ApiImplicitParam(name = "warehouse", value = "修改好的的仓库信息", paramType = "body", dataType = "Warehouse")
    @PostMapping("/update")
    public Map update(@RequestBody Warehouse warehouse) {
        warehouseService.update(warehouse);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getMap();
    }


    @ApiOperation(value = "是否启用", produces = "updateIsUsed")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "仓库编号"),
            @ApiImplicitParam(name = "isUsed", value = "是否启用")
    })
    @PostMapping("/updateIsUsed")
    public Map updateIsUsed(@RequestParam("id") Integer id, @RequestParam("isUsed") Boolean isUsed) {
        warehouseService.updateIsUsed(id, isUsed);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getMap();
    }

    @ApiOperation(value = "根据仓库编号删除", produces = "delete")
    @ApiImplicitParam(name = "id", value = "仓库编号")
    @PostMapping("/delete")
    public Map delete(@RequestParam("id") Integer id) {
        warehouseService.delete(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        return jsonResult.getMap();
    }

    @ApiOperation(value = "根据仓库名称和负责人模糊查询仓库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "仓库名称"),
            @ApiImplicitParam(name = "principal",value = "负责人")
    })
    @PostMapping("/selectLikeName")
    public Map SelectLikeName(@RequestParam(value = "name",required = false) String name,@RequestParam(value = "principal",required = false) String principal) {
        Map map = new HashMap();
        map.put("name",name);
        map.put("principal",principal);
        List<Warehouse> warehouses = warehouseService.selectLikeName(map);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(warehouses);
        return jsonResult.getMap();
    }


}
